package gui_layer;

import java.util.List;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import model_layer.Customer;
import ctr_layer.CustomerCtr;

@SuppressWarnings("serial")
public abstract class SetCustomerGUI extends UniversalAddWindow
{
	private List<Customer> all_customers;
	private CustomerCtr cust_ctr;
	protected String[] column_names = { "ID", "Name", "Phone number", "Email", "Address", "Zipcode", "City",  "Discount %" };
	
	public SetCustomerGUI()
	{
		cust_ctr = new CustomerCtr();
		setTitle("Set Customer");
		prepare_table(new Object[0][8], column_names);
		setBounds(100, 100, 450, 300);
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		get_non_deleted_customers();
		customize();
		this.addInternalFrameListener(new InternalFrameAdapter()
		{
			@Override
			public void internalFrameActivated(InternalFrameEvent arg0)
			{
				dynamically_search();
				
			}
		});
	}
	
	private void get_non_deleted_customers()
	{
		all_customers = cust_ctr.find_non_deleted_customers();
	}
	
	private void customize()
	{
		search_lbl.setText("Name/ID");
		btnAdd.setText("Set");
	}
	
	protected abstract void set_or_add();


	@Override
	protected void dynamically_search()
	{
		String text = search_field.getText();
		MyTableModel mtm = (MyTableModel) table.getModel();
		Vector<Object[]> vector = new Vector<Object[]>();
		Object[] data;
		for(Customer cust : all_customers)
		{
			if(cust.getName().startsWith(text) || cust.getId().startsWith(text))
			{
				
				data = new Object[8];
				data[0] = Integer.parseInt(cust.getId());
				data[1] = cust.getName();
				data[2] = cust.getPhone_nr();
				data[3] = cust.getEmail();
				data[4] = cust.getAddress();
				data[5] = cust.getZipcode();
				data[6] = cust.getCity();
				data[7] = cust.getDiscount();
				vector.add(data);
			}
		}
		Object[][] filling = new Object[vector.size()][8];
		for(int i = 0; i < vector.size(); i++)
		{
			filling[i] = vector.get(i);
		}
		mtm.setDataVector(filling, column_names);
	}

	@Override
	protected void show_non_deleted()
	{
		get_non_deleted_customers();
		dynamically_search();

	}

}
